package Fail3_17;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class FileDemo10 {
    /**
     * 按照文件内容关键词进行查找
     * 1.遍历文件，把文件加入list
     * @param
     */

        public static boolean isequalcontent(File f,String s){
            StringBuilder stringBuilder=new StringBuilder();
            try (InputStream stream=new FileInputStream(f);){
                Scanner scanner=new Scanner(stream,"UTF-8");
                while (scanner.hasNextLine()) {
                    stringBuilder.append(scanner.nextLine() + "\n");
                }
            }catch (IOException e){
                e.printStackTrace();
            }
            return stringBuilder.indexOf(s)!=-1;
        }
        public static void all_file(File file,List file2,String str) {
            File[] files = file.listFiles();
            for (File f : files) {
                if (f.isFile()) {
                    if(isequalcontent(f,str));
                }else if(f.isDirectory()){
                    all_file(f,file2,str);
                }
            }
            for (File f:files) {
                System.out.println(f.getAbsolutePath());
            }
        }

    public static void main(String[] args) {
        System.out.println("请输入您要查找的路径");
        Scanner scanner=new Scanner(System.in);
        String str=scanner.next();
        File file=new File(str);
        if(!file.exists()){
            System.out.println("路径名不正确");
        }
        System.out.println("请您输入您需要查找的内容");
        String str2=scanner.next();
        List<File> file2 = new ArrayList<>();
        all_file(file,file2,str);
    }
}
